package com.groupbuying.task.Jobs;


import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.groupbuying.task.mq.StoreInternalMQConfig;
import com.groupbuying.task.mq.StoreInternalMQProductor;
import com.groupbuying.task.mq.StoreMQConfig;
import com.groupbuying.task.mq.StoreMQProductor;
import com.groupbuying.task.utils.DateFormatHelper;
import com.groupbuying.task.utils.JSONUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

@Component
public class StoreJob {

    private static Logger log = LoggerFactory.getLogger(StoreJob.class);

    @Autowired
    private StoreInternalMQProductor storeInternalMQProductor;

    @Autowired
    private StoreMQProductor storeMQProductor;

    @Value("${task.scheduled.store.withdraw:false}")
    private String isDoWithdrawScheduled;

    @Value("${task.scheduled.store.rule:false}")
    private String isSetRule;

    @Async
    @Scheduled(cron = "0 30 14 * * ?")
    public void withdraw(){
        Calendar now = Calendar.getInstance();
        if(now.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || now.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || !isDoWithdrawScheduled.equals("true")){
            return;
        }

        Producer producer = storeMQProductor.getStoreMQProductor();
        log.info(DateFormatHelper.getNowTimeStr()+"=========执行提现==========");
        Map map = new HashMap();
        map.put("timeStamp",System.currentTimeMillis());
        Message message = new Message(StoreMQConfig.TOPIC, StoreMQConfig.WITHDRAW, JSONUtils.beanToJson(map).getBytes());
        producer.send(message);
        //storeFeign.withdraw();
    }
}
